package com.wyy.ems_vue.service.impl;/*
 **
 * @author yangyang
 *@date 2022/07/02 16:41
 **/

import com.wyy.ems_vue.dao.UserDao;
import com.wyy.ems_vue.entity.User;
import com.wyy.ems_vue.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public void register(User user) {
        User userName = userDao.findByUserName(user.getUsername());
        if (userName==null){
            user.setStatus("已激活");
            user.setRegisterTime(new Date());
            userDao.save(user);
        }else{
            throw new  RuntimeException("用户已存在");
        }


    }

    @Override
    public User login(User user) {
        User userName = userDao.findByUserName(user.getUsername());
        if (!StringUtils.isEmpty(userName)){
            if ((userName.getPassword()).equals(user.getPassword())){
                return  userName;
            }else {
                throw  new RuntimeException("密码输入不正确");
            }
        }else {
            throw  new RuntimeException("用户名输入错误");
        }
    }
}
